﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ButtonTree2
{
    class RibbonMenuStrategy
    {
        public RibbonMenuStrategy()
        {
        }

        public ContextMenuStrip Menu
        {
            get;
            set;
        }

        public bool IsMenuShowing
        {
            get;
            private set;
        }

        public void ShowMenu(Control control)
        {
            // 计算出下拉菜单所在位置并显示该菜单
            if (Menu != null && Menu.Items.Count > 0)
            {
                Rectangle rect = control.ClientRectangle;
                Point ptMenu = control.PointToScreen(new Point(rect.Left, rect.Bottom));
                IsMenuShowing = true;    // 表示控件继续高亮显示
                Menu.Closed += new ToolStripDropDownClosedEventHandler(Menu_Closed);    // 响应弹出菜单关闭事件
                Menu.Show(ptMenu);
            }
        }

        public void CloseMenu()
        {
            Menu.Close();
        }

        void Menu_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            IsMenuShowing = false;

            if (MenuClosedEventHandler != null)
            {
                MenuClosedEventHandler(sender, e);
            }
        }

        public EventHandler<ToolStripDropDownClosedEventArgs> MenuClosedEventHandler;
    }
}
